/**
 * Created with IntelliJ IDEA.
 * User: Katerina
 * Date: 20.01.13
 * Time: 19:59
 * To change this template use File | Settings | File Templates.
 */
package ru.gameChangers.homework.businessModels {
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

import mx.rpc.xml.XMLLoader;

public class ContentDataConstants {
    private static const C2C:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Consumer to Consumer", "Consumer to Consumer", new ImgLib.C2C(), ""));

    private static const C2B:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Consumer to Business", "Consumer to Business", new ImgLib.C2B(), ""));

    private static const C2G:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Consumer to Government", "Consumer to Government", new ImgLib.C2G(), ""));

    private static const B2C:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Business to Consumer", "Business to Consumer", new ImgLib.B2C(), ""));

    private static const B2B:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Business to Business", "Business to Business", new ImgLib.B2B(), ""));

    private static const B2G:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Business to Government", "Business to Government", new ImgLib.B2G(), ""));

    private static const G2C:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Government to Consumer", "Government to Consumer", new ImgLib.G2C(), ""));

    private static const G2B:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Government to Business", "Government to Business", new ImgLib.G2B(), ""));

    private static const G2G:ContentItemsContainer = new ContentItemsContainer(new ContentItem("Government to Government", "Government to Government", new ImgLib.G2G(), ""));

    {
        loadData();

    }

    public static function getData(i:int, j:int) : ContentItemsContainer {
        if (i == 0 && j == 0) {
            return C2C;
        }
        if (i == 0 && j == 1) {
            return C2B;
        }
        if (i == 0 && j == 2) {
            return C2G;
        }
        if (i == 1 && j == 0) {
            return B2C;
        }
        if (i == 1 && j == 1) {
            return B2B;
        }
        if (i == 1 && j == 2) {
            return B2G;
        }
        if (i == 2 && j == 0) {
            return G2C;
        }
        if (i == 2 && j == 1) {
            return G2B;
        }
        return G2G;
    }

    private static function loadData() : void {
        //var dataFile:String = "https://dl.dropbox.com/u/61779863/data.xml";
        var dataFile:String = "http://127.0.0.1/data.xml";
        var xmlLoader:URLLoader = new URLLoader();
        xmlLoader.addEventListener(Event.COMPLETE, parseXML);
        var request:URLRequest = new URLRequest(dataFile);
        xmlLoader.load(request);
    }

    private static function parseXML(e:Event):void {
        XML.ignoreWhitespace = true;
        var companies:XML = new XML(e.target.data);

        var modelsArray:Object = {c2c:C2C, c2b:C2B, c2g:C2G, b2c:B2C, b2b:B2B, b2g:B2G, g2c:G2C, g2b:G2B, g2g:G2G};
        var size:int = companies.company.length();
        for (var i:int = 0; i < companies.company.length(); i++) {

            var company:ContentItem = new ContentItem(companies.company[i].name.text(), companies.company[i].description.text()
                    , null, companies.company[i].logoUrl.text());

            var model:String = companies.company[i].model.text();
            if (model in modelsArray) {
                modelsArray[model].addNewItem(company);
            }
        }
    }

}
}
